package com.myosw.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import org.apache.commons.beanutils.BeanUtils;

import com.myosw.entity.PageShow;
import com.myosw.pojo.Filter;
import com.myosw.pojo.Filterlog;
import com.myosw.pojo.POJO;
import com.myosw.pojo.Urls;
import com.myosw.service.FilterService;

public class FilterAction extends BaseAction
{
	 @Override
	public void execute() throws ServletException, IOException {
		 root.put("menu", "filter");
		printFreemarker("default/filter.ftl", root);
		 
	}
	 
	 
	 public void addfilter() throws IllegalAccessException, InvocationTargetException, ServletException, IOException 
		{
		 root.put("menu", "filter");
			if (!request.isSubmit()) 
			{
				printFreemarker("default/addfilter.ftl", root);
				return;
			}
			
			Map<String, String[]> map = request.getParameterMap();
			Filter	filter = new Filter();
			BeanUtils.populate(filter , map);
			if(filterService.save(filter ))
			{
				//printFreemarker("default/service.ftl", root);
				sendRedirect(request.basePath()+"user/filter");
				return;
			}else {
				root.put("msg", "添加失败！");
				printFreemarker("default/addfilter.ftl", root);
				return;
			}

			
		}
	 
	 
	 public void show() 
	 {
		 root.put("menu", "filter");
		 String[] params = request.getParams();
		 long id = Long.parseLong(params[0]);
			int size = 10;
			int pagecount = 1;
			if(params!=null){
				if(params.length>1)
					pagecount = Integer.parseInt(params[1]);
				if(params.length>2)
					size = Integer.parseInt(params[2]);
			}
		 PageShow page = filterService.getFilterslog(pagecount, size,id);
		 Filter bean = new Filter();
		  Filter filter = bean.get(id);
		  List<Filterlog> list = filterService.getlogChange(id);
		  root.put("list", list);
		  root.put("filter", filter);
		 root.put("page", page);
		 
			printFreemarker("default/filtershow.ftl", root);
		
	}
	 
	 
	 FilterService filterService = new FilterService();
	 
}
